﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using HPAGMRestAPIWrapper;
using AGMLite.BaseClass;
using System.Reflection;
using System.Collections;
using System.Windows;
using AGMLite.Windows;
using System.Diagnostics;
using System.Windows.Threading;
using System.Threading;
using System.Windows.Media.Imaging;

namespace AGMLite.ViewModelClass
{
    public class EntityListViewModel : ViewModelBase
    {
        #region Properties
        public AGMItem Item
        {
            get
            {
                return _item;
            }
            set
            {
                if (value != _item)
                {
                    _item = value;
                    OnPropertyChanged("Item");
                }
            }
        }

        public Action CloseAction { get; set; }
        #endregion

        #region Private members
        private AGMItem _item;

        private RelayCommand<object> openEntityLinkCommand;
        private RelayCommand<object> viewEntityDetailCommand;
        #endregion

        public EntityListViewModel(AGMItem item)
        {
            foreach (AGMLiteEntity liteEntity in item.LiteEntityCollection.Where(o=> string.IsNullOrEmpty(o.Id)))
            {
                liteEntity.Id = liteEntity.Entity.GetField("id").Value;
                if (liteEntity.EntityType == Common.AGMLiteEnum.EntityType.ProjectTask)
                    liteEntity.Name = liteEntity.Entity.GetField("description").Value;
                else
                    liteEntity.Name = liteEntity.Entity.GetField("name").Value;
            }

            if (item.LiteEntityCollection.Where(o=>o.IsNew).Count() > 0)
                item.LiteEntityCollection.SortDescending(o => o.IsNew);
            _item = item; 
        }

        public void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
        {
        }

        #region Commands
        public System.Windows.Input.ICommand OpenEntityLinkCommand
        {
            get
            {
                if (openEntityLinkCommand == null)
                {
                    openEntityLinkCommand = new RelayCommand<object>(x => this.OpenEntityLink(x), x => this.CanOpenEntityLink(x));
                }
                return openEntityLinkCommand;
            }
        }

        public System.Windows.Input.ICommand ViewEntityDetailCommand
        {
            get
            {
                if (viewEntityDetailCommand == null)
                {
                    viewEntityDetailCommand = new RelayCommand<object>(x => this.ViewEntityDetail(x), x => this.CanViewEntityDetail(x));
                }
                return viewEntityDetailCommand;
            }
        }
        #endregion

        #region Implementation
        public void OpenEntityLink(object obj)
        {
            if (obj != null)
            {
                AGMLiteEntity selectedEntity = (AGMLiteEntity)obj;
                if (selectedEntity.IsNew)
                {
                    Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, (ThreadStart)delegate
                    {
                        selectedEntity.IsNew = false;
                        Item.UpdateNewStatusAndCache();
                    });
                }
                selectedEntity.OpenEntityPage();
            }
        }

        public void ViewEntityDetail(object obj)
        {
            if (obj != null)
            {
                AGMLiteEntity selectedEntity = (AGMLiteEntity)obj;
                if (selectedEntity.IsNew)
                {
                    Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, (ThreadStart)delegate
                    {
                        selectedEntity.IsNew = false;
                        Item.UpdateNewStatusAndCache();
                    });
                }

                selectedEntity.OpenEntityDetail();
            }
        }

        public bool CanViewEntityDetail(object obj)
        {
            if (obj == null)
                return false;

            return true;
        }

        public bool CanOpenEntityLink(object obj)
        {
            if (obj == null)
                return false;

            return true;
        }
 
        #endregion

        #region Private Methods
        
        #endregion

    }
}
